/**
 *
 * This is a AS3 Port of the MessagePack object serialization library
 *
 * MessagePack is a binary-based efficient object serialization library.
 * It enables to exchange structured objects between many languages like JSON.
 * But unlike JSON, it is very fast and small.
 *
 * For more information about MessagePack object serialization, visit: http://msgpack.org/
 *
 * @author	Joost Harts
 * @company De Monsters
 * @link 	http://www.deMonsters.com
 * @version 0.1
 *
 * 修改：黄新泽  在此基础上，加入处理object。 参考PHP msgpack算法实现
 *
 *
 *
 *
 * Copyright 2010, De Monsters
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

package org.serialization
{
    import flash.utils.ByteArray;


    final public class MessagePack
    {
        /*
        // fixed length
        const VALUE_SCALAR_NULL = 192; // xC0
        const VALUE_SCALAR_FALSE = 194; // xC2
        const VALUE_SCALAR_TRUE = 195; // xC3
        const VALUE_SCALAR_FLOAT = 202; // xCA
        const VALUE_SCALAR_DOUBLE = 203; // xCB
        // x00-x7f - integer 0-127 positive fixnum
        const VALUE_INT_FIX_NEGATIVE = 224; // 111XXXXX xE0-xFF -1 - -32 // unclear 11100000 = -1 ??
        const VALUE_INT_UNSIGNED_8 = 204; // xCC + 1 byte
        const VALUE_INT_UNSIGNED_16 = 205; // xCD + 2 byte
        const VALUE_INT_UNSIGNED_32 = 206; // xCE + 4 byte
        const VALUE_INT_UNSIGNED_64 = 207; // xCF + 8 byte
        const VALUE_INT_SIGNED_8 = 208; // xD0 + 1 byte
        const VALUE_INT_SIGNED_16 = 209; // xD1 + 2 byte
        const VALUE_INT_SIGNED_32 = 210; // xD2 + 4 byte
        const VALUE_INT_SIGNED_64 = 211; // xD3 + 8 byte
        // raw bytes
        const VALUE_RAW_FIX = 160; // xA0 101XXXXX + max 31 byte len
        const VALUE_RAW_16 = 218; // xDA save raw bytes up to (2^16)-1 bytes.
        const VALUE_RAW_32 = 219; // xDB save raw bytes up to (2^32)-1 bytes.
        // container
        const VALUE_LIST_FIX = 144; // x90 1001XXXX save an array up to 15 elements.
        const VALUE_LIST_16 = 220; // xDC save an array up to (2^16)-1 elements.
        const VALUE_LIST_32 = 221; // xDD save an array up to (2^32)-1 elements.
        const VALUE_MAP_FIX = 128; // x80 1000XXXX save a map up to 15 elements. odd elements are key and next element of the key is its associate value.
        const VALUE_MAP_16 = 222; // xDE save a map up to (2^16)-1 elements. odd elements are key and next element of the key is its associate value.
        const VALUE_MAP_32 = 223; // xDF save a map up to (2^32)-1 elements. odd elements are key and next element of the key is its associate value.
        */

        public static const UINT8:uint = 0xcc;

        public static const UINT16:uint = 0xcd;

        public static const UINT32:uint = 0xce;

        public static const UINT64:uint = 0xcf;

        public static const INT8:uint = 0xd0;

        public static const INT16:uint = 0xd1;

        public static const INT32:uint = 0xd2;

        public static const INT64:uint = 0xd3;

        public static const NIL:uint = 0xc0;

        public static const TRUE:uint = 0xc3;

        public static const FALSE:uint = 0xc2;

        public static const FLOAT:uint = 0xca;

        public static const DOUBLE:uint = 0xcb;

        public static const RAW16:uint = 0xda;

        public static const RAW32:uint = 0xdb;

        public static const ARRAY16:uint = 0xdc;

        public static const ARRAY32:uint = 0xdd;

        public static const MAP16:uint = 0xde;

        public static const MAP32:uint = 0xdf;

        public static const RAW_FIX:uint = 0xa0; //160

        public static const ARRAY_FIX:uint = 0x90; //144

        public static const MAP_FIX:uint = 0x80; //128

        public static const NEGATIVE_FIX:uint = 0xe0; //224

        /**
         * Encodes a object into a MessagePack ByteArray.
         *
         * @param o The object to create a MessagePack ByteArray for
         * @return the MessagePack ByteArray representing o
         * @langversion ActionScript 3.0
         * @playerversion Flash 9.0
         * @tiptext
         */
        public static function encode(o:*):ByteArray
        {
            return MessagePackEncode.encode(o);
        }

        /**
         * Decodes a MessagePack ByteArray into a native object.
         *
         * @param bytes The MessagePack ByteArray representing the object
         * @return A native object as specified by bytes
         * @langversion ActionScript 3.0
         * @playerversion Flash 9.0
         * @tiptext
         */
        public static function decode(bytes:ByteArray):*
        {
            return MessagePackDecode.decode(bytes);
        }

        /**
         * Displays a ByteArray into a HEX value String, can be usefull for debugging.
         *
         * @param bytes The ByteArray representing the object
         * @return A HEX String as specified by bytes
         * @langversion ActionScript 3.0
         * @playerversion Flash 9.0
         * @tiptext
         */
        public static function rawBytesToHexString(bytes:ByteArray):String
        {
            //To show hex value's of a byte array (doesnt add 0x)

            var s:String = "";
            while (bytes.position < bytes.length)
            {
                var n:String = bytes.readUnsignedByte().toString(16);
                if (n.length < 2)
                {
                    n = "0" + n;
                }
                s += n;
            }
            return "HEX: " + s;

        }
    }
}
